/**
 * 对象编辑模型
 * @param vueAppId vue绑定的容器Id
 * @param editFormId 表单Id
 * @param methods vue中的方法
 * @param computed vue中的属性计算器
 * @constructor
 */
function VueEditModel(vueAppId, editFormId, methods, computed) {
    methods = methods ? methods : {};
    computed = computed ? computed : {};
    this.vueAppId = vueAppId;
    this.editFormId = editFormId;
    this.data = {};
    var that = this;
    /**
     *  初始化vue
     * @returns {VueEditModel} 返回当前编辑模型对象
     */
    this.initVue = function (defaultData) {
        if (defaultData) {
            for (var key in defaultData) {
                that.data[key] = defaultData[key];
            }
        }
        that.data["bigRegions"] = CommonConst.BIGREGIONSOfArray;// 大区
        // 前后100年
        that.data["years"] = (function () {
            var years = new Array();
            var date = new Date();
            var nowYear = date.getFullYear();
            for (var i = nowYear - 100; i <= nowYear + 100; i++) {
                years.push(i);
            }
            return years;
        })();
        that.data["months"] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];//月份
        that.data["days"] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31];
        methods.reLoadDay = function (year, month) {
            var isR = (year % 100 != 0 && year % 4 == 0 || year % 400 == 0);//闰年
            var day;
            switch (parseInt(month)) {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    day = 31;
                    break;
                case 2:
                    day = isR ? 29 : 28;
                    break;
                default:
                    day = 30;
            }
            that.data.days = new Array();
            for (var i = 1; i <= day; i++) {
                that.data.days.push(i);
            }
            console.log(that.days);
        };
        /**
         * 实例化vue对象
         */
        that.vue = new Vue({
            el: '#' + that.vueAppId,
            data: that.data,
            methods: methods,
            computed: computed,
        });
        return that;
    };
    this.addData = function (key, objValue) {
        if (key) {
            that.data[key] = objValue;
        }
    };
    /**
     *  加载数据字典到vue中
     * @param dicKeys 需要提前加载的数据字典
     * @returns {VueEditModel} 返回当前编辑模型对象
     */
    this.loadDictionaryData = function (dicKeys, callback) {
        if (dicKeys && dicKeys.length > 0) {
            // 获取字典项:
            CommonService.getDictionarys(dicKeys, function (data) {
                for (var i = 0, len = dicKeys.length; i < len; i++) {
                    that.addData(dicKeys[i], data[dicKeys[i]]);
                }
                if (typeof callback === "function") {
                    callback(that);
                }
            });
        }
        return that;
    };
    /**
     * 加载非字典项的扩展数据
     * @param callback 会掉函数
     * @returns {VueEditModel} 返回当前编辑模型对象
     */
    this.loadExtendData = function (callback) {
        if (typeof callback === "function") {
            callback(that);
            //等待一秒初始化图片预览
           setTimeout(function(){
               Vue.nextTick(function () {
                   var imgPrev = $(".imgPrev");
                   for (var i = 0, size = imgPrev.size(); i < size; i++) {
                       initImgPrev(imgPrev.eq(i));
                   }
               });
           },2000);
        }
        return that;
    };

    /**
     * 关闭当前编辑的弹出层
     */
    this.closeDialog = function () {
        $("#" + that.editFormId).parents(".modal-content").find(".modal-header .close").click();
    };
    this.setDialogTitleSuffix = function (titleSuffix) {
        var $dialogTitle = $("#" + editFormId).parents(".modal-content").find(".modal-title");
        var dialogTitlePrefix = $dialogTitle.text();
        $dialogTitle.text(dialogTitlePrefix + titleSuffix);
    }
}
